package com.moblie.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.moblie.dao.UserDao;
import com.moblie.domain.User;
import com.moblie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 描述:
 *
 * @author 景远超
 * @create 2020-05-30 13:46
 */
@Transactional
@Service(interfaceClass = UserService.class)
public class UserServiceImpl implements UserService {
    @Autowired
   private UserDao userDao;
    @Override
    public User findByTelephone(String telephone) {
        User user= userDao.findByTelephone(telephone);
        return user;
    }

    @Override
    public void add(User user2) {
        userDao.add(user2);
    }

    /*
    gitHub授权登录
    * */
    @Override
    public User gitHubLogin(int gitHubId,String username) {
        User user = null;
        try {
            // 1.调用dao中findUserByGitHub方法,参数为该gitHubId
            user = userDao.findUserByGitHub(gitHubId);
            //2.判断该user是否为空.如果为空,添加该github用户信息到user表中
            if (user==null){
                // 2.1 创建一个user对象,设置username,phone,registtime
                 user = new User();
                 user.setUsername(username);
                 user.setPhone(null);
                 user.setRegistTime(new Date());
                 // 2.2 调用user的add方法,添加用户
                userDao.add(user);
                // 获取该user自动生成的id,往github中添加数据(userid和githubid)
                Map<String, Object> map = new HashMap<>();
                map.put("userId",user.getId());
                map.put("gitHubId",gitHubId);
                // 2.1 在往user和github中间表中添加用户和githubid绑定信息,参数
                userDao.addGitHub(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 最后返回user信息
        return user;
    }
}
